import { cancelWxBusiCoupon } from '@src/api/coupon'
Component({
  options: {
    addGlobalClass: true
  },
  properties: {
    couponDetail: {
      type: Object,
      value: null
    }
  },
  data: {
    showDialog: false,
    params: null,
    send_coupon_params: null,
    sign: null,
    send_coupon_merchant: null,
    codes: null,
  },
  methods: {
    dialogShow(prototype, params, codes) {
      console.log('prototype', prototype);
      let send_coupon_params;
      if (params.send_coupon_params) {
        send_coupon_params = JSON.parse(params.send_coupon_params)
      }
      this.setData({
        showDialog: true,
        prototype: prototype,
        params: params,
        send_coupon_params,
        codes
      })
      console.log('this.data.params', this.data.params)
    },
    hiddenDialog() {
      let codes = this.data.codes;
      let self = this;
      cancelWxBusiCoupon(codes).then(res => {
        if (res.code == 0) { self.setData({ showDialog: false }) }
      })
    },
    getNow() {
      this.setData({ showDialog: false })
      this.triggerEvent('getNowFunc')
    },
    updateCoupon(params) {
      let detail = params.detail;
      if (detail.errcode !== 'OK') {
        wx.showToast({
          title: '领券失败',
          icon: 'none',
          duration: 2000
        })
      } else {
        if (detail.send_coupon_result[0].code === 'SUCCESS') {
          this.triggerEvent('getNowFunc')
        } else {
          wx.showToast({
            title: detail.send_coupon_result[0].message,
            icon: 'none',
            duration: 2000
          })
        }
        this.setData({
          showDialog: false
        })
      }
      console.log('params 领取结果', params)
    },
    missCoupon() {
      this.setData({ showDialog: false });
      this.triggerEvent('missCoupon')
    }
  }
})
